package com.fm.controller;

import com.fm.entity.*;
import com.fm.service.*;
import com.fm.utils.KeyUtils;
import com.fm.utils.R;
import com.fm.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/30 0030.
 */
@RestController
@RequestMapping("/api/equipment")
public class EquipmentController extends MBaseController<Equipment, EquipmentService>{

    @Autowired
    ProductService productService;
    @Autowired
    ProductTopicService productTopicService;
    @Autowired
    EquipmentTopicService equipmentTopicService;
    @Autowired
    ProductFunctionService productFunctionService;

    /**
     * 根据设备私钥获取物模型
     * @param deviceSecret
     * @return
     */
    @RequestMapping("/getModalByKey")
    @ResponseBody
    public R getModalByKey(String deviceSecret) {
        try{
            Equipment equipment = this.baseService.getEquipmentBySecretKey(deviceSecret);
            Product product = productService.selectOneByExample(equipment.getProductId()).get();
            if (product == null) {
                return R.error("设备不存在！");
            }
            equipment.setProductKey(product.getProductKey());
            return R.ok().put("modal", product.getObjectModal()).put("device", equipment);
        }catch (RuntimeException e) {
            return R.error("请检查设备私钥是否正确！");
        }
    }

    @Override
    public Equipment beforeList(Map t) {
        Equipment equipment = new Equipment();
        equipment.setUserId(SessionUtil.getLoginUserId());
        return equipment;
    }

    @Override
    public void beforeInsert(Equipment equipment, HttpServletRequest request) {
        equipment.setCreateTime(new Date());
        equipment.setStatus(1);
        equipment.setUserId(SessionUtil.getLoginUserId());
        equipment.setDeviceSecret(KeyUtils.getRandonString(32));
    }

    @Override
    public void afterSave(Equipment equipment, HttpServletRequest request) {
        //先删除
        equipmentTopicService.deleteByEquipment(equipment.getId());
        ProductTopic productTopic = new ProductTopic();
        productTopic.setProductId(equipment.getProductId());
        List<ProductTopic> productTopicList = productTopicService.selectByExample(productTopic);
        for (ProductTopic topic : productTopicList) {
            EquipmentTopic equipmentTopic = new EquipmentTopic();
            equipmentTopic.setType(topic.getType());
            equipmentTopic.setEquipmentId(equipment.getId());
            equipmentTopic.setIsSystem(topic.getIsSystem());
            equipmentTopic.setValue(topic.getValue().replace("${deviceName}",equipment.getName()));
            equipmentTopicService.insert(equipmentTopic);
        }
    }

    @Override
    public void afterList(Page<Equipment> page) {
        for (Equipment equipment : page.getContent()) {
            equipment.setProductName(productService.selectOneByExample(equipment.getProductId()).get().getName());
        }
    }

    @RequestMapping("/fetchByProduct")
    @ResponseBody
    public R fetchByProduct(Integer id) {
        Equipment equipment = new Equipment();
        equipment.setProductId(id);
        List<Equipment> equipments = this.baseService.selectByExample(equipment);
        ProductFunction function = new ProductFunction();
        function.setParentId(0);
        function.setProductId(id);
        List<ProductFunction> functions = productFunctionService.selectByExample(function);
        return R.ok().put("data", equipments).put("funs", functions);
    }

    @RequestMapping("/getAllDevices")
    @ResponseBody
    public R getAllDevices(){
        Equipment equipment = new Equipment();
        equipment.setUserId(SessionUtil.getLoginUserId());
        List<Equipment> equipments = this.baseService.selectByExample(equipment);
        Product product;
        for(Equipment equip : equipments){
            product = new Product();
            product.setId(equip.getProductId());
            product = productService.selectByExample(product).get(0);
            equip.setProductKey(product.getProductKey());
            equip.setProductName(product.getName());
        }
        return R.ok().put("data", equipments).put("count", equipments.size());
    }



    @Override
    public void afterSelectOne(Equipment equipment) {
        Product product = productService.selectOneByExample(equipment.getProductId()).get();
        equipment.setProductName(product.getName());
        equipment.setProductKey(product.getProductKey());
    }
}
